<script type="text/javascript" src="<?= URL::base() ?>static/administration/js/ckeditor/ckeditor.js"></script>
<!-- elFinder CSS (REQUIRED) -->
<link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" type="text/css" media="screen" href="<?= URL::base() ?>static/administration/js/elfinder/css/elfinder.min.css">
<link rel="stylesheet" type="text/css" media="screen" href="<?= URL::base() ?>static/administration/js/elfinder/css/theme.css">

<!-- elFinder JS (REQUIRED) -->
<script type="text/javascript" src="<?= URL::base() ?>static/administration/js/elfinder/js/elfinder.min.js"></script>

<!-- elFinder translation (OPTIONAL) -->
<script type="text/javascript" src="<?= URL::base() ?>static/administration/js/elfinder/js/i18n/elfinder.ru.js"></script>

<script type="text/javascript">
	options = {
		url  : '<?= URL::base() ?>static/administration/js/elfinder/php/connector.php',
		lang : 'ru',
		'resizable' : false
	}
</script>

<!-- Yandex Maps -->
<script src="http://api-maps.yandex.ru/2.0/?load=package.full&amp;lang=ru-RU" type="text/javascript"></script>

<div class="well">
	<?= Form::open($actionLink, array('method' => 'post', 'enctype' => 'multipart/form-data', 'class' => 'form-horizontal')) ?>
		<? foreach ($params['fields'] as $key => $item): ?>
			<div class="control-group">
				<label class="control-label" for="input<?= $item['name'] ?>"><?= $item['title'] ?></label>
				<div class="controls">
					<?
						switch ($item['type']) {
							case 'text': // обычное текстовое поле
								echo Form::input($item['name'], $obj->$item['name'], array('id' => 'input'.$item['name'], 'placeholder' => $item['title'], 'class' => 'span3'));
								break;
							case 'password':
								echo Form::password($item['name'], '', array('id' => 'input_'.$item['name'], 'placeholder' => $item['title'], 'class' => 'span3'));
								break;
							case 'select': // обычный select с указанными параметрами
								echo Form::select($item['name'], $item['select_params']['values'], $obj->$item['name'], array('id' => 'input'.$item['name'], 'class' => 'span3'));
								break;
							case 'relation':
								if ($item['relation_params']['type'] == 'BELONGS_TO') { // select с параметрами из другой таблицы
									$arr = ORM::factory($item['relation_params']['model'])->find_all()->as_array('id', $item['relation_params']['field']);
									!empty($item['relation_params']['default']) ? $arr = $item['relation_params']['default'] + $arr : FALSE;
									echo Form::select($item['name'], $arr, $obj->$item['name'], array('id' => 'input'.$item['name'], 'class' => 'span3'));
								} elseif ($item['relation_params']['type'] == 'HAS_MANY') {
									$arr = ORM::factory($item['relation_params']['owned_table'])->find_all()->as_array('id', $item['relation_params']['field']);
									if ($action == 'edit')
										echo Form::select($item['name'], $arr, '', array('class' => 'span3')).' <input type="button" class="btn" id="add" value="Добавить" onclick="relationHasManyAdd(\''.URL::base().'administration/ahasmany/add\', this)" owner="'.$item['relation_params']['owner_table'].'" owned="'.$item['relation_params']['owned_table'].'" owner_id="'.$obj->id.'" /> <input type="button" class="btn btn-primary" id="showAll" value="Показать все" onclick="relationHasManyShow(\''.URL::base().'administration/ahasmany/show/'.$obj->id.'\', this)" owner="'.$item['relation_params']['owner_table'].'" owned="'.$item['relation_params']['owned_table'].'" owner_id="'.$obj->id.'" waymark="'.$item['relation_params']['field'].'" />';
									else
										echo '<input class="span3" id="disabledInput" type="text" placeholder="Можно добавлять только для существующих записей" disabled>';
								}

								break;
							case 'checkbox': // checkbox
								if ($obj->$item['name'] == 1 || !empty($item['on'])) {
									$checked = TRUE;
								} else {
									$checked = FALSE;
								}

								echo Form::checkbox($item['name'], 1, $checked, array('id' => 'input'.$item['name']));
								break;
							case 'image': // изображение
								echo '<div>'.Form::file($item['name']).'</div>';

								if (is_file($item['image_params']['dir'].'/admin/'.$obj->$item['name'])) {
									echo '
										<a target="_blank" class="thumbnail pull-left" href="'.$item['image_params']['url'].'/'.$obj->$item['name'].'">
											<img style="width: '.$item['thumbnails']['admin']['width'].'px" src="'.URL::site($item['image_params']['url'].'/'.'admin/'.$obj->$item['name']).'">
										</a>
									';
								}

								break;
							case 'tinymce': // текстовый редактор
								echo Form::textarea($item['name'], $obj->$item['name'], array('class' => 'ckeditor', 'id' => 'input'.$item['name']));
								break;
							case 'textarea': // textarea
								echo Form::textarea($item['name'], $obj->$item['name'], array('id' => 'input'.$item['name'], 'placeholder' => $item['title'], 'class' => 'span3', 'rows' => '5'));
								break;
							case 'datepicker': // календарь
								if (!$obj->$item['name']) {
									$default = date('Y-m-d');
								} else {
									$default = $obj->$item['name'];
								}

								echo Form::input($item['name'], $default, array('class' => 'field datepicker span3', 'id' => 'input'.$item['name']));
								break;
							case 'as_relation':
								if ($item['relation_params']['type'] == 'BELONGS_TO') {
									echo '
										<input type="hidden" value="'.$obj->$item['name'].'" />
										<span>'.$obj->$item['name'].'</span>
										<button class="btn advanced_select" type="button" onclick="show_advanced_select(this, \''.URL::site('administration/as_relation/'.$item['relation_params']['model']).'\', \''.$item['as_params']['tab_title'].'\', \'as_'.$item['name'].'\', \''.$item['relation_params']['field'].'\')">Выбрать</button>
									';
								}

								break;
							case 'yandexmap':
								echo '
									<div id="map" style="width:600px; height:300px"></div>
									<input type="text" name="coordinates" value="'.$obj->$item['name'].'" />
								';

								$coordinates = $obj->$item['name'];
								$coordinates = empty($coordinates) ? '43.238095889055714,76.89025894164453' : $coordinates;

								echo Model::factory('Afunctions')->yandex_map_init($coordinates);

								break;
						}

						echo !empty($item['hint']) ? '<span style="margin-left: 10px; color: red">'.$item['hint'].'</span>' : '';
					?>
				</div>
			</div>
		<? endforeach ?>
		<div class="control-group">
			<label class="control-label"></label>
			<div class="controls">
				<button type="submit" class="btn btn-primary">Сохранить</button>
			</div>
		</div>
	<?= Form::close() ?>
</div>